<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\LaborCompany;
use App\Services\LaborCompanyService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LaborCompanyController extends Controller
{
    protected $LaborCompanyService;
    public function __construct(LaborCompanyService $LaborCompanyService)
    {
        $this->LaborCompanyService = $LaborCompanyService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = $request->title;
        $pageSize = $request->pageSize ?: 10;
        $page = $request->page ?: 1;
        $page = $page * $pageSize - $pageSize;

        $sql = $this->LaborCompanyService->get($title);
        $sql->where('company_id',$request->company_id);
        $total = $sql->count();
        $data = $sql->skip($page)->take($pageSize)->orderBy('created_at','desc')->get();
        foreach ($data as $k => $v) {
            $data[$k]->registrant = $v->admin->name;
        }
        return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function all(Request $request)
    {
        $title = $request->title;
        $sql = $this->LaborCompanyService->get($title);
        $sql->where('company_id',$request->company_id);
        $data = $sql->orderBy('created_at','desc')->get();
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    public function show(Request $request)
    {
        $data = LaborCompany::find($request->id);
        if($data->start_date){
            $data->start_date = strtotime($data->start_date) * 1000;
        }
        if($data->end_date){
            $data->end_date = strtotime($data->end_date) * 1000;
        }

        $resources = $data->resources;
        $filesUrl = [];
        $business_certUrl = [];
        $security_certUrl = [];
        $legal_certUrl = [];
        $safety_certUrl = [];
        $human_resources_certUrl = [];
        foreach ($resources as $k => $v) {
            $arr = [];
            $arr['id'] = $v->id;
            $arr['name'] = $v->resource->name;
            $arr['url'] = $v->resource->url;
            if($data->files){
                $files = explode(',',$data->files);
                if(in_array($v->resource_id,$files)){
                    $filesUrl[] = $arr;
                }
            }

            $business_cert_arr = [];
            $business_cert_arr['id'] = $v->id;
            $business_cert_arr['name'] = $v->resource->name;
            $business_cert_arr['url'] = $v->resource->url;
            if($data->business_cert){
                $business_cert = explode(',',$data->business_cert);
                if(in_array($v->resource_id,$business_cert)){
                    $business_certUrl[] = $business_cert_arr;
                }
            }

            $security_cert_arr = [];
            $security_cert_arr['id'] = $v->id;
            $security_cert_arr['name'] = $v->resource->name;
            $security_cert_arr['url'] = $v->resource->url;
            if($data->security_cert){
                $security_cert = explode(',',$data->security_cert);
                if(in_array($v->resource_id,$security_cert)){
                    $security_certUrl[] = $security_cert_arr;
                }
            }

            $legal_cert_arr = [];
            $legal_cert_arr['id'] = $v->id;
            $legal_cert_arr['name'] = $v->resource->name;
            $legal_cert_arr['url'] = $v->resource->url;
            if($data->legal_cert){
                $legal_cert = explode(',',$data->legal_cert);
                if(in_array($v->resource_id,$legal_cert)){
                    $legal_certUrl[] = $legal_cert_arr;
                }
            }

            $safety_cert_arr = [];
            $safety_cert_arr['id'] = $v->id;
            $safety_cert_arr['name'] = $v->resource->name;
            $safety_cert_arr['url'] = $v->resource->url;
            if($data->safety_cert){
                $safety_cert = explode(',',$data->safety_cert);
                if(in_array($v->resource_id,$safety_cert)){
                    $safety_certUrl[] = $safety_cert_arr;
                }
            }

            $human_cert_arr = [];
            $human_cert_arr['id'] = $v->id;
            $human_cert_arr['name'] = $v->resource->name;
            $human_cert_arr['url'] = $v->resource->url;
            if($data->human_resources_cert){
                $human_resources_cert = explode(',',$data->human_resources_cert);
                if(in_array($v->resource_id,$human_resources_cert)){
                    $human_resources_certUrl[] = $human_cert_arr;
                }
            }
        }
        $data->filesUrl = $filesUrl;
        $data->business_certUrl = $business_certUrl;
        $data->security_certUrl = $security_certUrl;
        $data->legal_certUrl = $legal_certUrl;
        $data->safety_certUrl = $safety_certUrl;
        $data->human_resources_certUrl = $human_resources_certUrl;
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $POSTONLY = LaborCompany::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['admin_id'] = Auth::user()->id;
        $input['company_id'] = $request->company_id;
        $input['role_id'] = 0;
        $data = $this->LaborCompanyService->insert($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $POSTONLY = LaborCompany::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['id'] = $request->id;
        $input['admin_id'] = Auth::user()->id;
        $input['role_id'] = 0;
        $data = $this->LaborCompanyService->update($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        if(!$id){
            return response()->json(['code' => 404, 'message' => '请选择要操作的记录']);
        }
        $data = LaborCompany::find($id);
        $data->delete();
        return response()->json(['code' => 200, 'message' => '操作成功']);
    }



}
